package com.gitee.wsl.unit.angle.ext

import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.toDegrees


fun AngleUnit.clampAngle180() = degrees.coerceIn(-180.0, 180.0).toDegrees()
fun AngleUnit.clampAngle360() = degrees.coerceIn(0.0, 360.0).toDegrees()
fun AngleUnit.clampLatitude() = degrees.coerceIn(-90.0, 90.0).toDegrees()
fun AngleUnit.clampLongitude() = degrees.coerceIn(-180.0, 180.0).toDegrees()
/**
 * Restricts an angle to the range [-180, +180] degrees, clamping angles outside the range. Angles less than -180
 * are returned as -180, and angles greater than +180 are returned as +180. Angles within the range are returned
 * unmodified.
 *
 * @param degrees the angle to clamp in degrees
 *
 * @return the specified angle clamped to the range [-180, +180] degrees
 */

fun AngleUnit.Companion.clampAngle180(degrees: Double) = degrees.coerceIn(-180.0, 180.0)

/**
 * Restricts an angle to the range [0, 360] degrees, clamping angles outside the range. Angles less than 0 are
 * returned as 0, and angles greater than 360 are returned as 360. Angles within the range are returned unmodified.
 *
 * @param degrees the angle to clamp in degrees
 *
 * @return the specified angle clamped to the range [0, 360] degrees
 */

fun AngleUnit.Companion.clampAngle360(degrees: Double) = degrees.coerceIn(0.0, 360.0)

/**
 * Restricts an angle to the range [-90, +90] degrees, clamping angles outside the range. Angles less than -90 are
 * returned as -90, and angles greater than +90 are returned as +90. Angles within the range are returned
 * unmodified.
 *
 * @param degrees the angle to clamp in degrees
 *
 * @return the specified angle clamped to the range [-90, +90] degrees
 */

fun AngleUnit.Companion.clampLatitude(degrees: Double) = degrees.coerceIn(-90.0, 90.0)

/**
 * Restricts an angle to the range [-180, +180] degrees, clamping angles outside the range. Angles less than -180
 * are returned as 0, and angles greater than +180 are returned as +180. Angles within the range are returned
 * unmodified.
 *
 * @param degrees the angle to clamp in degrees
 *
 * @return the specified angle clamped to the range [-180, +180] degrees
 */

fun AngleUnit.Companion.clampLongitude(degrees: Double) = degrees.coerceIn(-180.0, 180.0)

fun AngleUnit.clamp(min: AngleUnit, max: AngleUnit) = AngleUnit.min(AngleUnit.max(this, min), max)